package com.hama.zea.tbUser.web;

import com.hama.iotrust.common.ValidatorException;
import com.hama.iotrust.common.mvc.ValidatorUtil;
import com.hama.iotrust.common.mvc.bind.annotation.FormModel;
import com.hama.iotrust.common.mybatis.criteria.Criteria;
import com.hama.iotrust.common.mybatis.criteria.Sort;
import com.hama.iotrust.common.mybatis.result.Pager;
import com.hama.zea.tbUser.entity.TbUser;
import com.hama.zea.tbUser.service.TbUserService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC Controler - 表：tb_user
 * @since 2016-07-06 12:01:07
 */
@Controller
@RequestMapping(value = "/tbUser")
public class TbUserAction {
	private static final Logger logger = LoggerFactory.getLogger(TbUserAction.class);

	@Autowired
	private TbUserService tbUserService;

	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/tbuser", method=RequestMethod.GET)
	public Object listPage() {
		return "tbuser";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/tbuser", method=RequestMethod.POST)
	@ResponseBody
	public Object listData(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			Criteria<TbUser> criteria = Criteria.create(TbUser.class);
			criteria.buildFromRequest(request);
			criteria.sortIfEmpty(Sort.asc("id"));
			Pager<TbUser> pager = tbUserService.findPage(criteria);
			resultMap.put("result", 1);
			resultMap.put("total", pager.getTotalRecords());
			resultMap.put("rows", pager.getList());
		} catch (Exception e) {
			resultMap.put("result", 0);
			resultMap.put("message", e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return resultMap;
	}

	/**
	 * 新增页面
	 */
	@RequestMapping(value = "/tbuseradd", method=RequestMethod.GET)
	public Object addPage() {
		return "tbuser_add";
	}

	/**
	 * 新增保存
	 */
	@RequestMapping(value = "/tbuseradd", method=RequestMethod.POST)
	@ResponseBody
	public Object doAdd(@FormModel("tbUser") TbUser tbUser) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			ValidatorUtil.validate(tbUser);
			tbUserService.insert(tbUser);
			resultMap.put("result", 1);
		} catch (ValidatorException ve) {
			resultMap.put("result", 0);
			resultMap.put("message", ve.getMessage());
		} catch (Exception e) {
			resultMap.put("result", 0);
			resultMap.put("message", e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return resultMap;
	}

	/**
	 * 修改页面
	 */
	@RequestMapping(value = "/tbuseredit", method=RequestMethod.GET)
	public Object editPage(String id) {
		ModelAndView model = new ModelAndView();
		try {
			TbUser tbUser = tbUserService.findByPrimaryKey(id);
			model.addObject("tbUser", tbUser);
			model.setViewName("tbuser_edit");
		} catch (Exception e) {
			model.addObject("exception", e.getMessage());
			model.setViewName("/error");
			logger.error(e.getMessage(), e);
		}
		return model;
	}

	/**
	 * 修改保存
	 */
	@RequestMapping(value = "/tbuseredit", method=RequestMethod.POST)
	@ResponseBody
	public Object doEdit(@FormModel("tbUser") TbUser tbUser) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			ValidatorUtil.validate(tbUser);
			tbUserService.update(tbUser);
			resultMap.put("result", 1);
		} catch (ValidatorException ve) {
			resultMap.put("result", 0);
			resultMap.put("message", ve.getMessage());
		} catch (Exception e) {
			resultMap.put("result", 0);
			resultMap.put("message", e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return resultMap;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/tbuserdelete", method=RequestMethod.POST)
	@ResponseBody
	public Object doDelete(@FormModel("ids") List<String> ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			tbUserService.batchDelete(ids);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", 0);
			resultMap.put("message", e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return resultMap;
	}
}